import { myRequest } from "@/api/ajax.js"
import Vue from 'vue'
import {change_key} from '@/api/addgoods/addgoods.js'
import {change_arr} from '@/api/addgoods/addgoods.js'
import {change_coupon} from '@/api/addgoods/addgoods.js'
export default {
	namespaced: true,

    state: {
        text:"我是goods模块下state.text的值",
		goodsInfor:[],   //所有的商品信息  [[],[]] 
		allGoods:[], //所有商品信息 [{leixing:'水果',goods:[]},{leixing:'零食',goods:[]}，]
		type:[],	//0：基础类型 1：附加类型 2 ：合并基础类型及附加类型，添加全部选项生成 3：优惠券
    },
    getters: {
		//管理员在商品管理那页，所遍历的数组
		goods(state){
			let arrOne= state.goodsInfor.reduce((res,element)=>{
				return res.concat(element)
			},[])
			console.log(arrOne)
			
			return arrOne
		},
		types(state){
			return state.type[2]
		},
		couponList(state){
			return state.type[3]
		}
		
    },
    mutations: {
		
		receiveGoods(state,result){			
			Vue.set(state.goodsInfor, 0, result)
/* 			Vue.set(state.goodsInfor, 1, result[1].goods)
 */		/* 	
			state.allGoods = result */
			// console.log('allGoods------------',state.allGoods)
		},
		receiveType(state,arr){
					Vue.set(state.type, 0, arr[0])
					Vue.set(state.type, 1, arr[1])
					Vue.set(state.type, 2, arr[2])
					Vue.set(state.type, 3, arr[3])
		/* 			Vue.set(state.goodsInfor, 1, result[1].goods)
		 */		/* 	
					state.allGoods = result */
/* 				 console.log('alltypes------------',state.type)
 */				},
		
    },
    actions: { 
		//获取商品信息（改进）
		async get_goods(context){
			let result = await myRequest({
				url:'/supermarket/goods/getGoods',
			})
			let resulttwo = await myRequest({
				url:'/supermarket/goods/getBaseType',
			})
			let test =[]
			let arr = result.data.items
			let basetype =resulttwo.data.items
			test = Object.keys(arr[0])
			test.push('basetypeTitle')
			arr =change_key(arr,test)
			arr = change_arr(arr,basetype)
			
			/* arr = change_arr(arr,basetype) */

			
			/* let type = Object.keys(result.data)
			 console.log(result.data)
			 let list = Object.values(result.data)
			console.log(Object.values(result))
			 let num = type.length */
		
			//foreach和map不会对空数组检测
			/* for(let k=0;k<arr.length;k++){
				arr[k] = {}
				arr[k].leixing = type[k]
				arr[k].goods = list[k]
			} */
			
			 console.log('get_goods',arr)
			context.commit('receiveGoods',arr)
		},
		async get_type(context){
			
			let resultone = await myRequest({
				url:'/supermarket/goods/getBaseType',
			})
			let resulttwo = await myRequest({
				url:'/supermarket/goods/getAppendType',
			})
			let resultthree = await myRequest({
				url:'/newtest/shop-mjq/selectAllmjq/1001',
			})
			let arr = []
			let all = [{
				    "typeId": "100001",
				    "typeTitle": "全部",
				    "shopID": "1001"
			}]
			let key = ["typeId","typeTitle","shopID"]
			arr[0] =resultone.data.items
			arr[1] =resulttwo.data.items
			arr[3] =resultthree.data.res
			//合并基础类型和附加类型，添加全部选项
			arr[2] = arr[0].concat(arr[1])
			arr[2] = all.concat(arr[2])
			//统一对象数组的key，方便遍历
			arr[2] = change_key(arr[2],["typeId","typeTitle","shopID"])
			// 匹配类型名称
			arr[3] = change_coupon(arr[3],arr[2])
			console.log('get_type',arr)
			context.commit('receiveType',arr)
			
		},
    },
	}